package org.minuframework.lang;

import java.util.Arrays;
import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.primitives.Booleans;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Chars;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;

/**
 * 数组工具类
 * @author 米mi
 */
public class ArrayUtils {
    
    // ------------------------------------------------------------------------
    /* 数组转集合 */
    // ------------------------------------------------------------------------
    
    /**
     * 数组转集合
     * @param  array byte[]
     * @return       List<Byte> 集合
     */
    public static List<Byte> asList(byte[] array) {
        List<Byte> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Bytes.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array short[]
     * @return       List<Short> 集合
     */
    public static List<Short> asList(short[] array) {
        List<Short> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Shorts.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array int[]
     * @return       List<Integer> 集合
     */
    public static List<Integer> asList(int[] array) {
        List<Integer> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Ints.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array long[]
     * @return       List<Long> 集合
     */
    public static List<Long> asList(long[] array) {
        List<Long> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Longs.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array float[]
     * @return       List<Float> 集合
     */
    public static List<Float> asList(float[] array) {
        List<Float> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Floats.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array double[]
     * @return       List<Double> 集合
     */
    public static List<Double> asList(double[] array) {
        List<Double> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Doubles.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array boolean[]
     * @return       List<Boolean> 集合
     */
    public static List<Boolean> asList(boolean[] array) {
        List<Boolean> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Booleans.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array char[]
     * @return       List<Character> 集合
     */
    public static List<Character> asList(char[] array) {
        List<Character> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Chars.asList(array);
        return list;
    }
    
    /**
     * 数组转集合
     * @param  array Object[]
     * @return       List<Object> 集合
     */
    public static List<Object> asList(Object[] array) {
        List<Object> list = Lists.newArrayList();
        if (array == null || array.length <= 0) { return list; }
        list = Arrays.asList(array);
        return list;
    }
    
    // ------------------------------------------------------------------------
    /* 合并数组 */
    // ------------------------------------------------------------------------
    
    /**
     * 数组合并
     * @param  list List<byte[]>
     * @return      byte[]
     */
    public static byte[] addBytes(List<byte[]> list) {
        byte[] arrays = new byte[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<short[]>
     * @return      short[]
     */
    public static short[] addShorts(List<short[]> list) {
        short[] arrays = new short[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<int[]>
     * @return      int[]
     */
    public static int[] addInts(List<int[]> list) {
        int[] arrays = new int[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<long[]>
     * @return      long[]
     */
    public static long[] addLongs(List<long[]> list) {
        long[] arrays = new long[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<float[]>
     * @return      float[]
     */
    public static float[] addFloats(List<float[]> list) {
        float[] arrays = new float[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<double[]>
     * @return      double[]
     */
    public static double[] addDoubles(List<double[]> list) {
        double[] arrays = new double[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<char[]>
     * @return      char[]
     */
    public static char[] addChars(List<char[]> list) {
        char[] arrays = new char[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<boolean[]>
     * @return      boolean[]
     */
    public static boolean[] addBooleans(List<boolean[]> list) {
        boolean[] arrays = new boolean[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        
        return arrays;
    }
    
    /**
     * 数组合并
     * @param  list List<Object[]>
     * @return      Object[]
     */
    public static Object[] addObjects(List<Object[]> list) {
        Object[] arrays = new Object[]{};
        if (list == null || list.size() <= 0) { return arrays; }
        if (list.size() == 1) { return list.get(0); }
        
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                arrays = list.get(list.size() - 1);
            } else {
                arrays = org.apache.commons.lang.ArrayUtils.addAll(arrays, list.get(i - 1));
            }
        }
        Arrays.sort(arrays);
        
        return arrays;
    }
    
    // ------------------------------------------------------------------------
    /* 包含 */
    // ------------------------------------------------------------------------
    
    public static boolean contains(byte[] array, byte param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(short[] array, short param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(int[] array, int param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(long[] array, long param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(float[] array, float param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(double[] array, double param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(char[] array, char param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(char[] array, String param) {
        return asStringList(array).contains(param);
    }
    
    public static boolean contains(boolean[] array, boolean param) {
        return asList(array).contains(param);
    }
    
    public static boolean contains(Object[] array, Object param) {
        return asList(array).contains(param);
    }
    
    // ------------------------------------------------------------------------
    // ------------------------------------------------------------------------
    
    public static List<String> asStringList(char[] array) {
        List<String> list = Lists.newArrayList();
        if (array.length <= 0) { return list; }
        for (int i = 0; i < array.length; i++) {
            list.add(String.valueOf(array[i]));
        }
        return list;
    }
    
    public static List<Integer> asIntegerList(char[] array) {
        List<Integer> list = Lists.newArrayList();
        if (array.length <= 0) { return list; }
        for (int i = 0; i < array.length; i++) {
            String str = String.valueOf(array[i]);
            if (StringUtils.isNumCase(str)) {
                list.add(Integer.parseInt(str));
            }
        }
        return list;
    }
    
}
